var checkNicknameResult = false;
var checkRegistEmailResult = false;
var checkPasswdResult = false;
var checkPasswdResult2 = false;
var checkIdResult = false;//base.js
var checkRealnameResult = false;

/**
 * 初始化注册框里的内容
 */
function initRegisterForm() {	
	//初始化 变量
	checkNicknameResult = false;
	checkRegistEmailResult = false;
	checkPasswdResult = false;
	checkPasswdResult2 = false;
	checkIdResult = false;
	checkRealnameResult = false;

	// 清空所有数据单
	$("#nickname").val("");
	$("#emailRegister").val("");
	$("#passwordRegister").val("");
	$("#passwordRegister2").val("");
	$("#idNumber").val("");
	$("#realName").val("");

	// 清空所有提示
	$("#nickname_font").empty();
	$("#email_font").empty();
	$("#passwordRegister_font").empty();
	$("#passwordRegister2_font").empty();
	$("#idNumber_font").empty();
	$("#realName_font").empty();

}

/**
 * 重新验证注册信息<br>
 * 全部正确返回true
 */
function checkFormRegister() {
	
	if (checkNicknameResult == true && checkRegistEmailResult == true
			&& checkPasswdResult == true && checkPasswdResult2 == true
			&& checkIdResult == true && checkRealnameResult == true) {
		return true;
	}
	else{
		return false;
	}
}

/**
 * 昵称不能为空
 */
function checkNickname(val) {
	nickname_font = $("#nickname_font");
	nickname_font.empty();
	if (val == undefined || val == "" || val == null) {
		// false
		checkNicknameResult = false;
		nickname_font.append("昵称不能为空");
		return false;
	} else {
		isNicknameExist(val, "nickname_font");
		return checkNicknameResult;
	}
}

/**
 * 检查身份证号
 */
function checkRegistIds(ids) {
	checkIdResult =  checkId(ids, 'idNumber_font');
}

/**
 * 检查真实姓名
 * 
 * @param realname
 */
function checkRealname(realname) {
	var realName_font = $("#realName_font");
	realName_font.empty();

	if (realname == "" || realname == undefined || realname == null) {
		checkRealnameResult = false;
		realName_font.append("姓名不能为空");
	} else {
		checkRealnameResult = true;
		realName_font
				.append("<img src='../images/register/true.png' class='r-true'>");
	}
}

/**
 * 检查密码
 * 
 * @param passwd
 */
function CharMode(iN){   
    if (iN>=48 && iN <=57) //数字   
    return 1;   
    if (iN>=65 && iN <=90) //大写字母   
    return 2;   
    if (iN>=97 && iN <=122) //小写   
    return 4;   
    else   
    return 8; //特殊字符   
    }   
function bitTotal(num){   
    modes=0;   
    for (var i=0;i<4;i++){   
        if (num & 1){  
            modes++;  
        }   
        num>>>=1;   
        }   
    return modes;   
} 
function checkStrong(sPW){   
    if (sPW.length<=4){  
        return 0; //密码太短   
        }  
        Modes=0;   
        for (var i=0;i<sPW.length;i++){   
            //测试每一个字符的类别并统计一共有多少种模式.   
            Modes|=CharMode(sPW.charCodeAt(i));   
        }   
      
    return bitTotal(Modes);   
  
} 
function checkPasswd(passwd){
	var passwordRegister_font = $("#passwordRegister_font");
	passwordRegister_font.empty();
	 var result="";
	 var safeFactor="";
    if (passwd==null||passwd==''){   
    	passwordRegister_font.append("密码不能为空");
    } else if (passwd.length < 6 || passwd.length > 18) {
		passwordRegister_font.append("密码长度应介于6-18");
		checkPasswdResult = false;
	}  else{   
       var S_level=checkStrong(passwd);   
        switch(S_level) {   
            case 0:   
                 result='安全系数危险！';
                 safeFactor='DANGER';
                 checkPasswdResult = true;
                 break;
            case 1:   
                 result='安全系数低！';   
                 safeFactor='LOW';
                 checkPasswdResult = true;
                break;   
            case 2:   
                 result='安全系数中！';
                 safeFactor='MIDDLE';
                 checkPasswdResult = true;
                break;   
            default:   
                 result='安全系数高！'; 
            	 safeFactor='HIGH';
            	 checkPasswdResult = true;
        }  
      passwordRegister_font.append("<img src='../images/register/true.png' class='r-true'>");
      passwordRegister_font.append(result);  
      $("#safeFactor").val(safeFactor);
      return true;   
    }
    return false;
    }   
/**
 * 密码确认
 */
function checkPasswd2(passwd2) {
	var passwordRegister = getElementById("passwordRegister");
	var passwordRegister2_font = $("#passwordRegister2_font");

	passwordRegister2_font.empty();

	if (passwordRegister.value != passwd2) {
		checkPasswdResult2 = false;
		passwordRegister2_font.append("密码不匹配");
	} else if (passwordRegister.value == passwd2 && passwd2.length >= 6
			&& passwd2.length <= 18) {
		checkPasswdResult2 = true;
		passwordRegister2_font
				.append("<img src='../images/register/true.png' class='r-true'>");
		return true;
	}
	return false;
}

/**
 * 对注册的邮箱的格式和是否被注册进行检查
 * 
 * @param email
 */
function checkRegistEmail(email, emailfont) {
	// 首先对邮箱的格式进行检查
	checkEmailResultXXX = false;

	// 邮箱格式的正则表达式
	var pattern = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	var html = "";

	//
	if (email == "" || email == undefined) {
		html = "邮箱不能为空";
		checkEmailResultXXX = false;
	}
	// 不符合邮箱的格式
	else if (!email.match(pattern)) {
		html = "邮箱的格式不正确";
		checkEmailResultXXX = false;
	}
	// 正确
	else {
		// html="正确";
		checkEmailResultXXX = true;
	}

	$("#" + emailfont).empty();

	if (checkEmailResultXXX == false) {
		checkRegistEmailResult = false;
		$("#" + emailfont).append(html);
	} else {
		isEmailExist(email, emailfont);
	}

}

/**
 * 邮箱是否被注册
 */
function isEmailExist(email, emailfont) {
	var email_font = $("#" + emailfont);
	var html = "";

	$.ajax({
				url : base_domain + '/base/isEmailExist.html',
				data : "email=" + email,
				type : 'post',
				dataType : 'json',
				contentType : "application/x-www-form-urlencoded;charset=utf-8",
				success : function(result) {
					email_font.empty();
					if (result == -1) {
						html = "该邮箱已被注册";
						checkRegistEmailResult = false;
					}else if(result==0) {
						html = "该邮箱真实不存在";
						checkRegistEmailResult = false;
					}
					else {
						html = "<img src='../images/register/true.png' class='r-true'>";
						checkRegistEmailResult = true;
					}
					email_font.append(html);
				},

				error : function() {
					checkRegistEmailResult = false;
					alert("连接服务器出错");
				}
			});

}


//检查昵称是否被注册
function isNicknameExist(nickname,nicknameFont){
	var nicknamefont = $("#" + nicknameFont);
	var html = "";
	
	$.ajax({
		url : base_domain + '/base/isNicknameExist.html',
		data : "nickname=" + nickname,
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
			nicknamefont.empty();
			if (result == "true") {
				html = "该昵称已被注册";
				checkNicknameResult = false;
			} else {
				html = "<img src='../images/register/true.png' class='r-true'>";
				checkNicknameResult = true;
			}
			nicknamefont.append(html);
		},

		error : function() {
			checkNicknameResult = false;			
			alert("连接服务器出错");
		}
	});
	
}

/**
 * 检查注册时的ID(身份证号)是否符合要求
 * 
 */
function checkRegisteID(idstr,showFont){
	checkIdResult = checkId(idstr, showFont);
}


// 注册
$(".register-submit").click(
				function() {
					if (checkFormRegister()) {
						var nickname = $("#nickname").val();
						var email = $("#emailRegister").val();
						var password = $("#passwordRegister").val();
						var idNumber = $("#idNumber").val();
						var realName = $("#realName").val();
						var sex = $('input[name="sex"]:checked').val();
						var role = $('input[name="role"]:checked').val();
						var major = $("#major").val();
						//普通用户没有职称
						if(role=="NORMAL"){
							major="无";
						}
						
						var safeFactor=$("#safeFactor").val();

						var datas = "nickname=" + nickname + "&email=" + email
								+ "&password=" + hex_md5(password)
								+ "&idNumber=" + idNumber + "&realName="
								+ realName + "&sex=" + sex + "&role=" + role
								+ "&major=" + major+"&safeFactor="+safeFactor;

						$.ajax({	url : base_domain + '/base/register.html',
									data : datas,
									type : 'post',
									dataType : 'json',
									contentType : "application/x-www-form-urlencoded;charset=utf-8",
									success : function(result) {
										// window.location.href =
										// "../base/login.html";

										if (result == "SUCCESS") {
											$(".register-close").click();
											alert("注册成功，请到注册邮箱里激活帐号");
										} else if (result == "FAILED") {
											alert("你填写的邮箱可能不存在，注册失败");
										} else {
											alert("网络异常,注册失败,请稍候再试");
										}
									},

									error : function() {
										alert("连接服务器出错,请稍候再试");
									}
								});
					}
				});
